require.config({
	paths: {
		"jquery":"jquery-1.11.3",
		"cookie":"jquery.cookie",
		"Animation":"requestAnimationFrame"
	}
});
//main.js
require(["jquery","cookie","Animation"],function($,cookie,Animation){
	$.getJSON("../json/LAMIU_one.json",function(json){
		//头部
		$("#headerCon a").hover(
			function(){
				$(this).css("color","rgb(204,204,204)");
			},
			function(){
				$(this).css("color","#000");
			}
		);
		//如果能获取到登录账号密码cookie值 就改掉主页的内容
		var users = $.cookie("loginedUsers") ? $.cookie("loginedUsers") : "" ;
		if(users){
			$(".loginIn a").html("欢迎您，尊敬的用户"+users);
			$(".registerOne a").html("");				
		}
		//search栏
		$("#hot").children().eq(1).html(json.search1);
		$("#hot").children().eq(2).html(json.search2);
		$("#hot").children().eq(3).html(json.search3);
		$("#forSale").attr("src",json.forSale);
		//console.log($("#headerCon a"));
		
		//menu栏
		var $VMenuList = $("#verticalMenu").children();
		$VMenuList.each(function(){
			$(this).css("background","url("+ json.MenubottomImg +") no-repeat center bottom");
		});
		//点击横向列表里的首页 跳转到首页
		$(".horMenuOne").click(function(){
			location.href = "LAMIU_one.html";
		});
		var $HMenuList = $("#horizontalMenu").children();
		$HMenuList.not("#horMenuOne").not("#allGoods").each(function(){
			$(this).hover(
				function(){
					$(this).css("color","#f19ec2");
				},
				function(){
					$(this).css("color","#000");
				}
			);
		});
		$("#verticalMenu li>a").hover(
			function(){
				$(this).css("font-weight","700");
			},
			function(){
				$(this).css("font-weight","300");
			}
		);
		//购物车
		$("#shoppingCart").hover(
			function(){
				$(this).first().css({"background-color":"white","border":"1px solid #e9e9e9e","height":"42px"});
				$("#goodsList").show();
			},
			function(){
				$("body").click(function(){
					$("#shoppingCart").first().css({"background":"none","border":"none","height":"40px"});
					$("#goodsList").hide();
				});
			}
		);
		//侧边栏
			$("#slidebar").find(".pinkA").hover(
				function(){
					$(this).css("background-color","#f53998");
				},
				function(){
					$(this).css("background-color","#000");
				}	
			);
			
			$("#cart_qqSevice").click(
				function(){
					$("#qqSevice_show").show();
				}
			);
			$("#back_btn").click(
				function(){
					$("#qqSevice_show").hide();
				}
			);
			/*回到顶部*/
			$("#slide_backToTop").click(
				function(){
					$("body").scrollTop(0);
				}
			);
			//注册
			$(".registerTwo").click(function(){
				//获取用户名密码
				var usn = $(".username").val();
				var pwd = $(".pass").val();
				var pwdA = $(".passAgain").val();
				
				//用户名不能为空
				if(!usn){
					alert("用户名不能为空");
					return;
				}
				//判断两次密码是否相同
				if(pwd != pwdA){
					alert("两次输入密码不相同,请重试");
					return;
				}
				//检测一下用户是否已经存在
				var users = $.cookie("registerUsers") ? $.cookie("registerUsers") : "";
				users = convertStrToObj(users);
				if(usn in users){
					alert("用户已被注册");
					return;
				}else{
					//注册成功 设置用户信息的cookie
					//如users[茜] = wx; 此时为对象
					users[usn] = pwd;
					//将用户信息对象转化为字符串,以便设置cookie
					userStr = convertObjToStr(users);
					$.cookie("registerUsers",userStr,{expires : 7,path : "/"});
					alert('注册成功');
				}
			});
			$(".backToVerify").click(function(){
				location.href = "login.html";
			});
			
			
			//将字符串转为对象
			function convertStrToObj(str){
				if(!str){ //如果是空字符串
					return {}; //返回空对象
				}
				var users = str.split(":");
				var obj = {};
				for(var i = 0; i < users.length; i ++){
					var userData = users[i].split(",");
					obj[userData[0]] = userData[1];
				}
				return obj;
			}
			
			//将对象转为字符串
			function convertObjToStr(obj){
				var str = "";
				//遍历对象
				for(var usn in obj){
					var pwd = obj[usn];
					if(str){
						str += ":";
					}
					str += usn + ',' + pwd;
				}
				return str;
			}

	});
});	